package com.scholarship.modules.admin.controller;

import com.scholarship.common.entity.RetCode;
import com.scholarship.common.entity.RetResponse;
import com.scholarship.common.entity.RetResult;
import com.scholarship.modules.admin.dto.RankingListDto;
import com.scholarship.modules.admin.dto.RankingQueryDto;
import com.scholarship.common.dto.ResultListDto;
import com.scholarship.modules.admin.service.RankingListService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @Author Z.J.H
 * @create 2021/5/28 10:21
 */
@Api(tags = {"专业排名展示控制层"})
@RestController
public class RankingController {

    @Autowired
    private RankingListService rankingListService;

    /**
     * 获取专业排名列表查询条件下拉框信息
     * @return
     */
    @ApiOperation("获取专业排名列表查询条件下拉框信息")
    @GetMapping("/admin/ranking/query")
    public RetResult getRankingQuery() {
        try {
            Map<String, List<String>> map = rankingListService.getRankingQuery();
            return RetResponse.makeRsp(RetCode.SUCCESS,"获取查询信息成功",map);
        }catch (Exception e) {
            return RetResponse.makeRsp(RetCode.INTERNAL_SERVER_ERROR,"未知错误");
        }
    }

    /**
     * 获取所有排名信息
     * @param pageNum 页码
     * @param pageSize 每页显示数量
     * @return
     */
    @ApiOperation("获取所有排名信息")
    @GetMapping("/admin/ranking/all")
    public RetResult listAllStudentInfo(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                        @RequestParam(value = "pageSize", defaultValue = "20") Integer pageSize) {
        try {
            ResultListDto<RankingListDto> result = rankingListService.listAllRanking(pageNum,pageSize);
            return RetResponse.makeOKRsp(result);
        } catch (Exception e) {
            return RetResponse.makeErrRsp("列表信息获取失败");
        }
    }

    /**
     * 根据查询条件查询排名信息
     * @param pageNum
     * @param pageSize
     * @param rankingQueryDto
     * @return
     */
    @ApiOperation("根据查询条件排名学生信息")
    @GetMapping("/admin/ranking/list")
    public RetResult listStudentInfo(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                     @RequestParam(value = "pageSize", defaultValue = "20") Integer pageSize,
                                     RankingQueryDto rankingQueryDto) {
        try {
            ResultListDto<RankingListDto> result = rankingListService.listRankingByQuery(pageNum,pageSize,rankingQueryDto);
            return RetResponse.makeOKRsp(result);
        } catch (Exception e) {
            e.printStackTrace();
            return RetResponse.makeErrRsp("列表信息获取失败");
        }
    }

    /**
     * 根据搜索查询排名信息
     * @param pageNum
     * @param pageSize
     * @param searchStr
     * @return
     */
    @ApiOperation("根据搜索查询排名信息")
    @GetMapping("/admin/ranking/search")
    public RetResult listStudentInfo(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                     @RequestParam(value = "pageSize", defaultValue = "20") Integer pageSize,
                                     @RequestParam("searchStr") String searchStr) {
        try {
            ResultListDto<RankingListDto> result = rankingListService.listRankingBySearch(pageNum,pageSize,searchStr);
            return RetResponse.makeOKRsp(result);
        } catch (Exception e) {
            e.printStackTrace();
            return RetResponse.makeErrRsp("列表信息获取失败");
        }
    }

    /**
     * 即刻刷新排名信息
     * @return
     */
    @ApiOperation("即刻刷新排名信息")
    @GetMapping("/admin/ranking/refresh")
    public RetResult refreshRankingInformation() {
        try {
            rankingListService.refreshRankingInformation();
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            return RetResponse.makeErrRsp("刷新失败");
        }
    }
}
